package ext.generic.part;

import java.util.ArrayList;
import java.util.Hashtable;

public class BomArrayList extends ArrayList
{

    private Hashtable bomOjbectHash;
    private final String DELIM = "_";

    public BomArrayList()
    {
        bomOjbectHash = new Hashtable();
    }

    public boolean add(BomObject bomObject)
    {
        if(bomObject == null)
        {
            return false;
        }
        if(!super.add(bomObject))
        {
            return false;
        } else
        {
            ArrayList indexArrayList = (ArrayList)bomOjbectHash.get(bomObject.getNumber());
            int index = super.lastIndexOf(bomObject);
            String indexStr = String.valueOf(index);
            indexArrayList.add(indexStr);
            bomOjbectHash.put(bomObject.getNumber(), indexArrayList);
            return true;
        }
    }

    public void clear()
    {
        super.clear();
        bomOjbectHash.clear();
    }

    public boolean contains(BomObject bomObject)
    {
        if(bomObject == null)
        {
            return false;
        } else
        {
            return contains(bomObject.getNumber());
        }
    }

    public boolean contains(String bomObjectNumber)
    {
        if(bomObjectNumber == null)
        {
            return false;
        } else
        {
            return bomOjbectHash.containsKey(bomObjectNumber);
        }
    }

    public BomArrayList get(BomObject bomObject)
    {
        if(bomObject == null)
        {
            return null;
        } else
        {
            return get(bomObject.getNumber());
        }
    }

    public BomArrayList get(String bomObjectNumber)
    {
        if(bomObjectNumber == null)
        {
            return null;
        }
        if(!bomOjbectHash.containsKey(bomObjectNumber))
        {
            return null;
        }
        BomArrayList indexArrayList = (BomArrayList)bomOjbectHash.get(bomObjectNumber);
        int index = -1;
        String indexStr = "";
        BomArrayList array = new BomArrayList();
        for(int i = 0; i < indexArrayList.size(); i++)
        {
            indexStr = indexArrayList.get(i).toString();
            index = Integer.parseInt(indexStr);
            array.add(super.get(index));
        }

        return array;
    }

    public BomArrayList get(String typeValue, String typeName)
    {
        if(typeValue == null)
        {
            return null;
        }
        BomArrayList array = new BomArrayList();
        BomObject obj = null;
        String value = "";
        for(int i = 0; i < super.size(); i++)
        {
            if(super.get(i) == null || !(super.get(i) instanceof BomObject))
            {
                continue;
            }
            obj = (BomObject)super.get(i);
            value = BomObject.getAttributeValue(obj, typeName);
            if(value != null && value.equals(typeValue))
            {
                array.add(obj);
            }
        }

        return array;
    }

    public int indexOf(BomObject bomObject)
    {
        if(bomObject == null)
        {
            return -1;
        } else
        {
            return indexOf(bomObject.getNumber());
        }
    }

    public int indexOf(String bomObjectNumber)
    {
        if(bomObjectNumber == null)
        {
            return -1;
        }
        if(!bomOjbectHash.containsKey(bomObjectNumber))
        {
            return -1;
        } else
        {
            ArrayList indexArrayList = (ArrayList)bomOjbectHash.get(bomObjectNumber);
            String indexStr = indexArrayList.get(0).toString();
            int index = Integer.parseInt(indexStr);
            return index;
        }
    }

    public int lastIndexOf(BomObject bomObject)
    {
        if(bomObject == null)
        {
            return -1;
        } else
        {
            return indexOf(bomObject.getNumber());
        }
    }

    public int lastIndexOf(String bomObjectNumber)
    {
        if(bomObjectNumber == null)
        {
            return -1;
        }
        if(!bomOjbectHash.containsKey(bomObjectNumber))
        {
            return -1;
        } else
        {
            ArrayList indexArrayList = (ArrayList)bomOjbectHash.get(bomObjectNumber);
            String indexStr = indexArrayList.get(indexArrayList.size() - 1).toString();
            int index = Integer.parseInt(indexStr);
            return index;
        }
    }

    public BomArrayList remove(BomObject bomObject)
    {
        if(bomObject == null)
        {
            return null;
        } else
        {
            return remove(bomObject.getNumber());
        }
    }

    public BomArrayList remove(String bomObjectNumber)
    {
        if(bomObjectNumber == null)
        {
            return null;
        }
        if(!bomOjbectHash.containsKey(bomObjectNumber))
        {
            return null;
        }
        BomArrayList indexArrayList = (BomArrayList)bomOjbectHash.get(bomObjectNumber);
        int index = -1;
        String indexStr = "";
        BomArrayList array = new BomArrayList();
        for(int i = indexArrayList.size() - 1; i >= 0; i--)
        {
            indexStr = indexArrayList.get(i).toString();
            index = Integer.parseInt(indexStr);
            array.add((BomObject)super.remove(index));
        }

        bomOjbectHash.remove(bomObjectNumber);
        return array;
    }
}
